<?php

namespace app\parking\controller\admin;

use think\admin\Controller;
use think\Request;
use app\parking\model\ParkingCompany as CompanyModel;

class Company extends Controller
{
    /**
     * 公司管理列表页面
     */
    public function index(Request $request)
    {
        $this->title = '公司管理';
        return $this->fetch();
    }

    /**
     * 获取公司管理列表
     */
    public function get(Request $request)
    {
        $query = CompanyModel::order('id desc');

        // 搜索过滤
        if ($name = $request->get('name')) {
            $query->where('name', 'like', "%{$name}%");
        }

        $data = $query->paginate(10)->toArray();
        return json([
            'code' => 0,
            'msg' => '',
            'count' => $data['total'],
            'data' => $data['data']
        ]);
    }

    /**
     * 保存公司
     */
    public function save(Request $request)
    {
        $data = $request->post();
        if (isset($data['id']) && !empty($data['id'])) {
            CompanyModel::update($data, ['id' => $data['id']]);
            $this->success('公司更新成功');
        } else {
            CompanyModel::create($data);
            $this->success('公司添加成功');
        }
    }

    /**
     * 新增公司页面
     */
    public function add()
    {
        return $this->fetch('form');
    }

    /**
     * 编辑公司页面
     */
    public function edit($id)
    {
        $this->assign('vo', CompanyModel::find($id));
        return $this->fetch('form');
    }

    /**
     * 删除公司
     */
    public function delete($id)
    {
        CompanyModel::destroy($id);
        $this->success('公司删除成功');
    }
}
